<?php
!defined ('P_W') && exit('Forbidden');
class PW_Groupbuy {
	var $_budget = "budget";
	var $_area = "acreage";
	var $house_field;
	var $db;
	
	function PW_Groupbuy(){
		$this->__construct();
	}
	
	function __construct(){
		global $db,$house_field;
		$this->db = & $db;
		$this->house_field = & $house_field;
	}
	
	/**
	 * @return unknown_type
	 */
	function getBudgetTypes(){
		$result = array();
		if ($this->house_field) {
			$this->house_field[$this->_budget] && $result = $this->house_field[$this->_budget];
		}
		if (empty($result)) {
			$result = $this->_getHouseFieldsByTypes($this->_budget);
		}
		return $result;
	}
	
	/**
	 * @return unknown_type
	 */
	function getAreaTypes(){
		$result = array();
		if ($this->house_field) {
			$this->house_field[$this->_area] && $result = $this->house_field[$this->_area];
		}
		if (empty($result)) {
			$result = $this->_getHouseFieldsByTypes($this->_area);
		}
		return $result;
	}
	
	/**
	 * @param $type
	 * @return unknown_type
	 */
	function _getHouseFieldsByTypes($type){
		$result = array();
		$_sql = "SELECT f.* FROM pw_house_fields f WHERE f.ifopen = '1' AND f.type=".pwEscape($type)." ORDER BY f.vieworder ASC";
		$query = $this->db->query($_sql);
		while ($rt = $this->db->fetch_array($query)) {
			$result[$rt['id']] = $rt['fieldvalue'];
		}
		return $result;
	}
	
	/**
	 * 对搜索结果进行统计
	 * @param $data	搜索结果数据列表
	 * @return array
	 */
	function statsHouseGroupbuy($data){
		$result = array();
		if (is_array($data) && !empty($data)) {
			$budgetstats = $areastats = array();
			foreach ($data as $key => $value) {
				!$result['gender'] && $result['gender'] = array();
				$result['gender'][$value['gender']] = (int)$result['gender'][$value['gender']] + 1;
				!$result['budget'] && $result['budget'] = array();
				$result['budget'][$value['budget']] = (int)$result['budget'][$value['budget']] + 1;
				!$result['area'] && $result['area'] = array();
				$result['area'][$value['area']] = (int)$result['area'][$value['area']] + 1;
			}
		}
		return $result;
	}
	
}
?>